<?
@session_start();

require_once("conn.php");

$id = $_REQUEST["id"];

?>
<script>
	
	function setItemExport(index){
		$("#eProduct").val($("#pName_"+index).html());
		$("#eBarcode").val($("#pBarcode_"+index).html());
		$("#eAmount").val($("#pAmount_"+index).html());
		$("#eUnit").val($("#pUnit_"+index).html());
		$("#eIndex").val(index);
		$("#eProductID").val($("#pID").val());
	}

	function saveExport(){
		var orderid = $("#eOrderid").val();
		var orderdate = $("#eOrderdate").val();
		var productid = $("#eProductID").val();
		var barcode = $("#eBarcode").val();
		var product = $("#eProduct").val();		
		var amount = $("#eAmount").val();
		var unit = $("#eUnit").val();
		var dealer = $("#eDealer").val();
		var orderby = $("#eOrderBy").val();

		var data = "&orderid="+orderid+"&orderdate="+orderdate+"&productid="+productid+"&barcode="+barcode+"&product="+product+"&amount="+amount+"&unit="+unit+"&dealer="+dealer+"&orderby="+orderby+"&t="+Math.random();
		
		if(validateExport()){
			$.ajax({
			   type: "POST",
			   url: "process.php?cmd=addExport",
			   data: data,
			   success: function(msg){
					$("#showProgress").html(msg);					
					if(msg.match("Saved")){
						$("#eProductID").val("");
						$("#eProduct").val("");
						$("#eBarcode").val("");
						$("#eAmount").val("");
						$("#eUnit").val("");
						$('#rows_'+$("#eIndex").val()).remove();
					}
			   }
			});
		}
	}

	function finishExport(id){
		$.ajax({
		   type: "POST",
		   url: "process.php?cmd=finishExport",
		   data: "&id="+id+"&t="+Math.random(),
		   success: function(msg){
				alert(msg);
				if(msg.match("Saved")){
					viewOrderExport(id);
				}
		   }
		});
	}

	function validateExport(){
		var product = $("#eProduct").val();
		var barcode = $("#eBarcode").val();
		var amount = $("#eAmount").val();
		var unit = $("#eUnit").val();
		var dealer = $("#eDealer").val();
		var orderby = $("#eOrderBy").val();

		if(product == ""){
			alert("โปรดกรอกฃื่อสินค้า");
			return false;
		}

		if(amount == "" || amount < 1){
			alert("โปรดกรอกจำนวนที่ถูกต้อง");
			return false;
		}

		if(unit == ""){
			alert("โปรดกรอกหน่วย");
			return false;
		}

		if(dealer == ""){
			alert("โปรดเลือกผู้ขายสินค้า");
			return false;
		}

		if(orderby == ""){
			alert("โปรดกรอกฃื่อผู้สั่ง");
			return false;
		}
		
		return true;

	}

</script>
<h3>รายการสั่งสินค้าจากฐานข้อมูล</h3>
<div id="showProgress">&nbsp;</div>
<table width="100%" style="border:0px solid black;">
	<tr>
		<td>ผู้สั่ง</td>
		<td>
			<input type="text" id="eOrderBy">						
		</td>
	</tr>
</table>
<table width="100%" style="border:0px;">
	<tr>
		<td>
			<table>
				<tr>
					<td>ชื่อสินค้า</td>
					<td><input type="text" id="eProduct"></td>
				</tr>
			</table>
		</td>
		<td>
			<table>
				<tr>
					<td>รหัสสินค้า</td>
					<td><input type="text" id="eBarcode"></td>
				</tr>
			</table>
		</td>
	</tr>
	<tr>
		<td>
			<table>
				<tr>
					<td>จำนวน</td>
					<td><input type="text" id="eAmount"></td>
				</tr>
			</table>
		</td>
		<td>
			<table>
				<tr>
					<td>หน่วย</td>
					<td><input type="text" id="eUnit"></td>
				</tr>
			</table>
		</td>
	</tr>
	<tr>
		<td>
			<table>
				<tr>
					<td>สถานที่</td>
					<td>
						<select id="eDealer">
							<option value="">โปรดเลือกหน่วยงาน</option>
							<?
								$sql = "SELECT* FROM pch_dealer";
								$query = mysql_query($sql);
								while($row = mysql_fetch_array($query)){
							?>
							<option value="<?=$row["id"]?>"><?=$row["dealer"]?></option>
							<?
								}
							?>
						</select>
					</td>
				</tr>
			</table>
		</td>
		<td>
			<?
				$sql = "SELECT order_date FROM pch_order WHERE id='".$id."';";
				$query = mysql_query($sql);
				$row = mysql_fetch_assoc($query);
			?>
			<input type="hidden" id="eIndex" value="">
			<input type="hidden" id="eProductID" value="">
			<input type="hidden" id="eOrderid" value="<?=$id?>">
			<input type="hidden" id="eOrderdate" value="<?=$row["order_date"]?>">
			<input type="button" onclick="saveExport()" value="OK">
		</td>
	</tr>	
</table>
<hr>
<table id="ordertList">
	<tr>
		<td>ลำดับ</td>
		<td>รหัสสินค้า</td>
		<td>รายการสินค้า</td>
		<td>จำนวน</td>
		<td>หน่วย</td>
		<td>จัดการ</td>
	</tr>
	<?
	$sql = "SELECT a.product_id as id,b.barcode as barcode,product_name,amount,unit_name FROM (pch_order_detail as a join pch_product as b ON a.product_id=b.id) join pch_unit as c on a.unit=c.id WHERE order_id='".$id."' AND a.product_id not in (select product_id FROM pch_orderexport where order_id='".$id."');";
	$query = mysql_query($sql);
	$i=1;
	while($row = mysql_fetch_array($query)){
	?>
		<tr id="rows_<?=$i?>">
			<td><?=$i?></td>
			<td><div id="pBarcode_<?=$i?>"><?=$row["barcode"]?></div></td>
			<td><div id="pName_<?=$i?>"><?=$row["product_name"]?></div></td>
			<td><div id="pAmount_<?=$i?>"><?=$row["amount"]?></div></td>
			<td><div id="pUnit_<?=$i?>"><?=$row["unit_name"]?></div></td>
			<td><input type="hidden" id="pID" value="<?=$row["id"]?>"><input type="button" onclick="setItemExport('<?=$i?>')" value="Add"></td>
		</tr>


	<?
		$i++;

	}
?>
	<tr>
		<td colspan="6"><input type="button" value="Save" onclick="finishExport('<?=$id?>');"></td>
	</tr>
</table>